<template>
  <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px" 
      >
      <el-form-item label="订单编号" prop="orderCode">
        <el-input style="display:none" />
          <el-input
            v-model="queryParams.orderCode"
            placeholder="请输入订单编号"
            clearable
            class="!w-240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="出库单号" prop="exCode">
          <el-input
            v-model="queryParams.exCode"
            placeholder="请输入出库单号"
            clearable
            class="!w-240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <!-- <el-button
            type="primary"
            plain
            @click="handleFun"
          >
            作废
          </el-button> -->
        </el-form-item>
      </el-form>
    </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
      <el-table v-loading="loading" :data="list" class="cen">
        <el-table-column type="index" label="序号" fixed="left" align="center" width="55" />
        <el-table-column label="订单编号" align="center" prop="orderCode" />
        <el-table-column label="出库单号" align="center" prop="exCode" />
        <el-table-column label="操作时间" align="center" prop="operatingTime" />
        <el-table-column label="领料状态" align="center" prop="materialRequisitionStatus" >
          <template #default="scope">
            <div class="flex items-center justify-center">{{ scope.row.materialRequisitionStatus==='0' ? '正常':'作废' }}</div>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="160">
          <template #default="scope">
            <div class="flex items-center justify-center">
              <el-button
                type="primary"
                link
                @click="openDetail(scope.row.exCode)"
              >查看
              </el-button>
              <el-button
              v-hasPermi="['system:menu:delete']"
              link
              type="danger"
              @click="handleDelete(scope.row.exCode)"
            >
              作废
            </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <!-- 查看对话框 -->
    <materialPickDetail ref="formRef" @success="getList" />
  </template>
  
  <style>
    .el-table .warning-row {
      --el-table-tr-bg-color: var(--el-color-warning-light-9)!important;
    }
    .el-table .success-row {
      background-color: var(--el-color-success-light-9)!important;
    }
  </style>

  <script lang="ts" setup>
  import download from '@/utils/download'
  import * as materialPickReportApi from '@/api/hcxly/materialPickReport/Index'
  import materialPickDetail from './materialPickDetail.vue'
  
  defineOptions({ name: 'materialPick' })
  
  const message = useMessage() // 消息弹窗
  const { t } = useI18n() // 国际化
  
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const list = ref([]) // 列表的数据

  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    orderCode: '',
    exCode: '',
    dictType: 'materialRequisitionStatus'
  })
  const queryFormRef = ref() // 搜索的表单

  const states = ref([])
  /** 查询列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await materialPickReportApi.getmaterialPick(queryParams)
      list.value = data.list
      total.value = data.total
      // getListState()
      //查询状态
      // const dataState = await materialPickReportApi.materialPickState(queryParams)
      // states.value = dataState.list
      // if(total.value>0){
      //   for(let i in states.value){
      //     // console.log(i)
      //     list.value[i].exStatus = states.value[i].label
      //   }
      // }
      // console.log(list.value)
    } finally {
      loading.value = false
    }
  }
  
  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }
  
  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }

  
  //   /** 查询列表状态 */
  //   const getListState = async () => {
  //   loading.value = true
  //   try {
  //     const dataState = await materialPickReportApi.materialPickState(queryParams)
  //     states.value = dataState.list
  //     // console.log(list.value)
  //   } finally {
  //     loading.value = false
  //   }
  // }
  

  const DataCode = reactive({
    exCode: ''
  })
  /** 作废按钮操作 */
  const handleDelete = async (code) => {
    DataCode.exCode = code
    try {
      // 作废的二次确认
      await message.confirm('确认作废该领料单吗？')
      // 发起作废
      await materialPickReportApi.getmaterialDelete(DataCode)
      message.success('作废成功！')
      // 刷新列表
      await getList()
    } catch {}
  }
  /** 判断表格单元背景色 */
  // const tableRowClassName = (row) => {
  //   console.log(row.row.materialStatus)
  //   if (row.row.materialStatus === '2') {
  //     return 'warning-row'
  //   } else if (row.row.materialStatus === '1') {
  //     return 'success-row'
  //   }
  //   return ''
  // }

  /** 初始化 **/
  onMounted(() => {
    getList()
  })
  
  /** 查看详情操作 */
  const formRef = ref()
  const openDetail = (id: string) => {
    console.log(id)
    formRef.value.open(id)
  }
  </script>
